package 力扣._00周赛._367周赛.问题4;

import java.math.BigInteger;

public class Solution {
    public int[][] constructProductMatrix(int[][] grid) {
        int n = grid.length;
        int m = grid[0].length;
        int[][] p = new int[n][m];

        BigInteger product = BigInteger.ONE;
        BigInteger modValue = BigInteger.valueOf(12345);

        for (int[] ints : grid) {
            for (int j = 0; j < m; j++) {
                product = product.multiply(BigInteger.valueOf(ints[j]));
                if (product.compareTo(modValue) > 0)
                    product = product.mod(modValue);
            }
        }

        // 计算每个元素的乘积
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                p[i][j] = product.divide(BigInteger.valueOf(grid[i][j])).mod(modValue).intValue();
            }
        }
        return p;
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        int[][] grid = {{1, 2}, {3, 4}};
        int[][] result = solution.constructProductMatrix(grid);
        // 输出结果
        for (int[] row : result) {
            for (int val : row) {
                System.out.print(val + " ");
            }
            System.out.println();
        }
    }
}

